package com.UI;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.Model.StudentDBModel;



public class ViewCourseActivity extends Activity {
    /** Called when the activity is first created. */
		
	String cNo;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   //     setContentView(R.layout.view_course_activity);
        
        /** Author: Mohammed  */
 
       	Bundle extras = getIntent().getExtras();
    	cNo = extras.getString("CourseNo");
       
    	 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
         setContentView(R.layout.view_course_activity);
     	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.view_course_titlebar);
     	
     	((TextView)findViewById(R.id.titleCourse)).setText("Course " + cNo);
     	
     	findViewById(R.id.back).setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			ViewCourseActivity.this.finish();

    		}
    	});

     	
     	
     	
    	TextView output=(TextView) this.findViewById(R.id.printCourses);
    	
    	final StudentDBModel model = new StudentDBModel(this);
    	
    	StringBuilder course = model.displayCourse(cNo);
    	
        output.setText(course);
        
        
        /** Author : Mohammed
         *  register this activity as a button event listener
         */
        
		Button btnClassLink = (Button) findViewById(R.id.btnClassLink);
		btnClassLink.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				
		    	StringBuilder link = model.getWebLink(cNo);		        
		    	String webLink = link.toString();
				if(webLink.length() <= 7) {
			  	      
			 		   AlertDialog alertDialog = new AlertDialog.Builder(ViewCourseActivity.this).create();	        			
			 		   alertDialog.setTitle("Warning");	        		  
			 		   alertDialog.setMessage("Please provide a Course Website for this Course");	        		  	        		  
			 		   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			 		  
			 		   public void onClick(DialogInterface dialog, int which) {
					       return;
					  } }); 
					  alertDialog.show();
					  
					}
					
				else {
				// go to the home page
	        	Intent i = new Intent(ViewCourseActivity.this,ClassWebsite.class);
	        	i.putExtra("CourseNum", cNo);
	        	startActivity(i);
				//finish the activity to return to the calling event
				}
			}
		});
               
        /**
         * Author : Steve
         *  register this activity as a button event listener
         */
		
		Button btnAddClassEvent = (Button) findViewById(R.id.btnFacHoursList);
		btnAddClassEvent.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent viewFacHours = new Intent(ViewCourseActivity.this, FacultyHoursList.class);
				viewFacHours.putExtra("CourseNum", cNo);
				startActivity(viewFacHours);
			}
		});
		/*
		 * Author: Sugat Mahanti
		 * Registers this activity as button listener for adding study group information
		 */
		
//		Button btnAddStudyInfo = (Button)findViewById(R.id.btnAddStudyInfo);
//		btnAddStudyInfo.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View component) {
//				// go to the home page
//				Intent addEvent = new Intent(ViewCourseActivity.this, AddStudyInfo.class);
//				addEvent.putExtra("CourseNum", cNo);
//				startActivity(addEvent);
//				//finish the activity to return to the calling event
//			}
//		});
		
		Button btnStudyInfoList = (Button) findViewById(R.id.btnStudyInfoList);
		btnStudyInfoList.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent eventList = new Intent(ViewCourseActivity.this, StudyInfoList.class);
				eventList.putExtra("CourseNum", cNo);
				startActivity(eventList);
			}
		});
		
		
		
		
		
        /**
         * Author : Steve
         *  register this activity as a button event listener
         */
		
		Button btnClassEventList = (Button) findViewById(R.id.btnClassEventList);
		btnClassEventList.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent eventList = new Intent(ViewCourseActivity.this, ClassEventList.class);
				eventList.putExtra("CourseNum", cNo);
				startActivityForResult(eventList, 1);
			}
		});
		
		Button btnGoHome = (Button) findViewById(R.id.btnHome);
		btnGoHome.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the home page
				Intent goHome = new Intent(ViewCourseActivity.this, HomePage.class);
				startActivity(goHome);
				//finish the activity to return to the calling event
				ViewCourseActivity.this.finish();
			}
		});

        /**
         * 
         *  register Notes as a button event listener
         */
		
		Button btnAddNotes = (Button) findViewById(R.id.btnAddNotes);
		btnAddNotes.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View component) {
				// go to the notes page
				Intent pickEvent = new Intent(ViewCourseActivity.this, NotepadAddDelete.class);
				pickEvent.putExtra("CourseNum", cNo);
				startActivity(pickEvent);
		
			}
		});
		
		
    }
    
    // this activity is called when startActivityForResult is called
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        final StudentDBModel model = new StudentDBModel(this);
        TextView output=(TextView) this.findViewById(R.id.printCourses);
        StringBuilder course = model.displayCourse(cNo);   	
        output.setText(course);
    }	
    
    
}